

package top.continew.admin.question.user.service.questionset.factory;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import top.continew.admin.question.model.question.QuestionSet;
import top.continew.admin.question.user.service.questionset.entity.AbstractQuestionSet;
import top.continew.admin.question.user.service.questionset.enums.QuestionSetTypeEnum;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

public class QuestionSetFactory {

    public static AbstractQuestionSet of(Object objectId, Integer type) {
        Function<Object, AbstractQuestionSet> function = Objects.requireNonNull(QuestionSetTypeEnum.getEnumByCode(type))
            .getFunction();
        return function.apply(objectId);
    }

    public static AbstractQuestionSet of(Object objectId) {
        QuestionSet questionSet = new QuestionSet().selectOne(Wrappers.lambdaQuery(QuestionSet.class)
            .or((a) -> a.eq(QuestionSet::getObjectId, objectId).or(b -> b.eq(QuestionSet::getId, objectId))));
        if (questionSet == null) {
            return null;
        }
        QuestionSetTypeEnum questionSetTypeEnum = QuestionSetTypeEnum.getEnumByCode(Math.toIntExact(questionSet
            .getType()));
        if (questionSetTypeEnum != null) {
            if (questionSetTypeEnum == QuestionSetTypeEnum.SPECIAL_PRACTICE) {
                return questionSetTypeEnum.getFunction()
                    .apply(Map.of("userId", questionSet.getUserId(), "knowledgeId", questionSet.getObjectId()));
            }
            return questionSetTypeEnum.getFunction().apply(objectId);
        }
        return null;
    }
}
